package com.example.shipload_e.activitys.menu_activity;

import com.example.shipload_e.R;
import com.example.shipload_e.ShipMentWelcomeActivity;
import com.example.shipload_e.util.SharePreferenceUtil;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.Selection;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

public class ModifyPasswordActivity extends Activity {
	private ProgressDialog	       progressDialog;
	private SharePreferenceUtil	   spu;
	private ModifyPasswordActivity	THISAC	= ModifyPasswordActivity.this;
	private ImageButton	           mGoback;
	private EditText	           oldInfo, newInfo, confirmInfo;
	private CheckBox	           showStateCk;
	private Button	               confirmBt;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.modify_password_main);
		progressDialog = new ProgressDialog(THISAC);
		spu = new SharePreferenceUtil(THISAC);
		initView();
		com.example.shipload_e.util.SysApplication.getInstance().addActivity(this);
	}

	private void initView() {
		// 输入密码显示或者隐藏
		oldInfo = (EditText) findViewById(R.id.old_info);
		newInfo = (EditText) findViewById(R.id.new_info);
		confirmInfo = (EditText) findViewById(R.id.confirm_info);
		oldInfo.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
		newInfo.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
		confirmInfo
		        .setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
		showStateCk = (CheckBox) findViewById(R.id.show_state);
		showStateCk.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton arg0,boolean isChecked) {
				if(isChecked) {
					oldInfo.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
					newInfo.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
					confirmInfo.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
				} else {
					oldInfo.setInputType(InputType.TYPE_CLASS_TEXT
					        | InputType.TYPE_TEXT_VARIATION_PASSWORD);
					newInfo.setInputType(InputType.TYPE_CLASS_TEXT
					        | InputType.TYPE_TEXT_VARIATION_PASSWORD);
					confirmInfo.setInputType(InputType.TYPE_CLASS_TEXT
					        | InputType.TYPE_TEXT_VARIATION_PASSWORD);
				}
				// 光标位置
				Editable etable = confirmInfo.getText();

				Selection.setSelection(etable, etable.length());
			}
		});

		mGoback = (ImageButton) findViewById(R.id.m_goback);
		mGoback.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				startActivity(new Intent(THISAC, ShipMentWelcomeActivity.class));
			}
		});
		confirmBt = (Button) findViewById(R.id.confirm_button);
		confirmBt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if(oldInfo.getText().length() <= 0 || newInfo.getText().length() <= 0
				        || confirmInfo.getText().length() <= 0) {
					Toast.makeText(THISAC, "不能为空，请重试。", Toast.LENGTH_SHORT).show();
				} else if(!newInfo.getText().toString().equals(confirmInfo.getText().toString())) {
					Toast.makeText(THISAC, "新密码和确认新密码不匹配。", Toast.LENGTH_SHORT).show();
				}
			}
		});
	}
}
